package com.tingyuge.base.design.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * @Created by xiaodao
 * 聚合类
 */
public class ConcreteMyAggregate {

    private List<Object> list = new ArrayList<>();

    public void add(Object obj){
        list.add(obj);
    }

    public  void remove(Object obj){
        list.remove(obj);
    }
    public MyIterator getIterator(){
        return new ConcreteIterator();
}

    /**
     * 内部类返回聚合类的迭代器...
     */
 private class ConcreteIterator implements  MyIterator{

        private int cursor;

        @Override
        public void first() {
            cursor =0;
        }

        @Override
        public Object next() {
            try {
                return list.get(cursor);
            } finally {
                if(cursor <list.size()){
                    cursor++;
                }
            }
        }

        @Override
        public boolean hasNext() {
            if(cursor <list.size()){
                return true;
            }
            return  false;
        }

        @Override
        public boolean isFirst() {
            return cursor==0? true:false;
        }

        @Override
        public boolean isLast() {
            return cursor ==(list.size()-1)?true:false;
        }

        @Override
        public Object getCurrentObj() {
            Object o = list.get(cursor);
            next();
            return o;
        }
    }

}
